/*     */ package com.zimbra.cs.service.account;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AccountConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.EntrySearchFilter;
/*     */ import com.zimbra.cs.account.EntrySearchFilter.Multi;
/*     */ import com.zimbra.cs.account.EntrySearchFilter.Operator;
/*     */ import com.zimbra.cs.account.EntrySearchFilter.Single;
/*     */ import com.zimbra.cs.account.EntrySearchFilter.Visitor;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.gal.GalUtil;
/*     */ import com.zimbra.cs.gal.GalExtraSearchFilter;
/*     */ import com.zimbra.cs.gal.GalExtraSearchFilter.FilteredGalSearchResultCallback;
/*     */ import com.zimbra.cs.gal.GalExtraSearchFilter.GalExtraQueryCallback;
/*     */ import com.zimbra.cs.gal.GalSearchConfig;
/*     */ import com.zimbra.cs.gal.GalSearchControl;
/*     */ import com.zimbra.cs.gal.GalSearchParams;
/*     */ import com.zimbra.cs.gal.GalSearchQueryCallback;
/*     */ import com.zimbra.cs.gal.GalSearchResultCallback;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import com.zimbra.soap.account.type.MemberOfSelector;
/*     */ import com.zimbra.soap.type.GalSearchType;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class SearchGal
/*     */   extends GalDocumentHandler
/*     */ {
/*     */   public boolean needsAuth(Map<String, Object> context)
/*     */   {
/*  54 */     return true;
/*     */   }
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context) throws ServiceException
/*     */   {
/*  59 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  60 */     Account account = getRequestedAccount(getZimbraSoapContext(context));
/*     */     
/*  62 */     if (!canAccessAccount(zsc, account)) {
/*  63 */       throw ServiceException.PERM_DENIED("can not access account");
/*     */     }
/*  65 */     return searchGal(zsc, account, request);
/*     */   }
/*     */   
/*     */   private static Element searchGal(ZimbraSoapContext zsc, Account account, Element request)
/*     */     throws ServiceException
/*     */   {
/*  71 */     String ref = request.getAttribute("ref", null);
/*     */     
/*     */ 
/*  74 */     String name = null;
/*  75 */     if (ref == null) {
/*  76 */       name = request.getAttribute("name");
/*     */     }
/*     */     
/*  79 */     EntrySearchFilter filter = GalExtraSearchFilter.parseSearchFilter(request);
/*     */     
/*  81 */     String typeStr = request.getAttribute("type", "all");
/*  82 */     GalSearchType type = GalSearchType.fromString(typeStr);
/*  83 */     boolean needCanExpand = request.getAttributeBool("needExp", false);
/*  84 */     boolean needIsOwner = request.getAttributeBool("needIsOwner", false);
/*  85 */     MemberOfSelector needIsMember = MemberOfSelector.fromString(request.getAttribute("needIsMember", MemberOfSelector.none.name()));
/*     */     
/*     */ 
/*     */ 
/*  89 */     boolean needSMIMECerts = request.getAttributeBool("needSMIMECerts", false);
/*     */     
/*  91 */     String galAcctId = request.getAttribute("galAcctId", null);
/*     */     
/*  93 */     GalSearchParams params = new GalSearchParams(account, zsc);
/*     */     
/*  95 */     if (ref == null) {
/*  96 */       params.setQuery(name);
/*     */     }
/*     */     else {
/*  99 */       params.setSearchEntryByDn(ref);
/*     */     }
/* 101 */     params.setType(type);
/* 102 */     params.setRequest(request);
/* 103 */     params.setNeedCanExpand(needCanExpand);
/* 104 */     params.setNeedIsOwner(needIsOwner);
/* 105 */     params.setNeedIsMember(needIsMember);
/* 106 */     params.setNeedSMIMECerts(needSMIMECerts);
/* 107 */     params.setResponseName(AccountConstants.SEARCH_GAL_RESPONSE);
/*     */     
/* 109 */     if (galAcctId != null) {
/* 110 */       params.setGalSyncAccount(Provisioning.getInstance().getAccountById(galAcctId));
/*     */     }
/*     */     
/* 113 */     if (filter != null) {
/* 114 */       params.setExtraQueryCallback(new SearchGalExtraQueryCallback(filter));
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 121 */     if (request.getAttribute("limit", null) == null) {
/* 122 */       request.addAttribute("limit", 100L);
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 131 */     params.setResultCallback(new SearchGalResultCallback(params, filter, null, null));
/*     */     
/* 133 */     GalSearchControl gal = new GalSearchControl(params);
/* 134 */     gal.search();
/* 135 */     return params.getResultCallback().getResponse();
/*     */   }
/*     */   
/*     */   private static class SearchGalResultCallback extends GalExtraSearchFilter.FilteredGalSearchResultCallback
/*     */   {
/*     */     private SearchGalResultCallback(GalSearchParams params, EntrySearchFilter filter, Set<String> attrs) {
/* 141 */       super(filter, attrs);
/*     */     }
/*     */   }
/*     */   
/*     */   private static class SearchGalExtraQueryCallback extends GalExtraSearchFilter.GalExtraQueryCallback implements GalSearchQueryCallback
/*     */   {
/*     */     public SearchGalExtraQueryCallback(EntrySearchFilter filter) {
/* 148 */       super();
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     public String getZimbraLdapSearchQuery()
/*     */     {
/* 162 */       SearchGal.GenLdapQueryByNamedFilterVisitor visitor = new SearchGal.GenLdapQueryByNamedFilterVisitor();
/* 163 */       this.filter.traverse(visitor);
/* 164 */       String query = visitor.getFilter();
/* 165 */       return StringUtil.isNullOrEmpty(query) ? null : query;
/*     */     }
/*     */   }
/*     */   
/*     */   private static class GenLdapQueryByNamedFilterVisitor implements EntrySearchFilter.Visitor {
/*     */     StringBuilder mLdapFilter;
/*     */     boolean mEncounteredError;
/*     */     
/*     */     public GenLdapQueryByNamedFilterVisitor() {
/* 174 */       this.mLdapFilter = new StringBuilder();
/*     */     }
/*     */     
/*     */     public String getFilter() {
/* 178 */       return this.mEncounteredError ? null : this.mLdapFilter.toString();
/*     */     }
/*     */     
/*     */     public void visitSingle(EntrySearchFilter.Single term)
/*     */     {
/* 183 */       EntrySearchFilter.Operator op = term.getOperator();
/* 184 */       String namedFilter = term.getLhs();
/* 185 */       String value = term.getRhs();
/*     */       
/* 187 */       namedFilter = namedFilter + "_" + op.name();
/* 188 */       String filter = null;
/*     */       try {
/* 190 */         String filterDef = GalSearchConfig.getFilterDef(namedFilter);
/* 191 */         if (filterDef != null) {
/* 192 */           filter = GalUtil.expandFilter(null, filterDef, value, null);
/*     */         }
/*     */       } catch (ServiceException e) {
/* 195 */         ZimbraLog.gal.warn("cannot find filter def " + namedFilter, e);
/*     */       }
/*     */       
/* 198 */       if (filter == null)
/*     */       {
/* 200 */         this.mEncounteredError = true;
/* 201 */         return;
/*     */       }
/*     */       
/* 204 */       boolean negation = term.isNegation();
/*     */       
/* 206 */       if (negation) {
/* 207 */         this.mLdapFilter.append("(!");
/*     */       }
/*     */       
/* 210 */       this.mLdapFilter.append(filter);
/*     */       
/* 212 */       if (negation) {
/* 213 */         this.mLdapFilter.append(')');
/*     */       }
/*     */     }
/*     */     
/*     */     public void enterMulti(EntrySearchFilter.Multi term)
/*     */     {
/* 219 */       if (term.isNegation()) this.mLdapFilter.append("(!");
/* 220 */       if (term.getTerms().size() > 1) {
/* 221 */         if (term.isAnd()) {
/* 222 */           this.mLdapFilter.append("(&");
/*     */         } else
/* 224 */           this.mLdapFilter.append("(|");
/*     */       }
/*     */     }
/*     */     
/*     */     public void leaveMulti(EntrySearchFilter.Multi term) {
/* 229 */       if (term.getTerms().size() > 1) {
/* 230 */         this.mLdapFilter.append(')');
/*     */       }
/* 232 */       if (term.isNegation()) {
/* 233 */         this.mLdapFilter.append(')');
/*     */       }
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/account/SearchGal.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */